<template>
  <div class="main-container">
    <div class="main-content">
      <div class="row">
        <div class="col-sm-10 col-sm-offset-1">
          <div class="login-container">

            <div class="header">
              <br><br><br>
              <h1>
                <i class="ace-icon fa fa-leaf green"></i>
                <span class="red">在线视频</span>
                <span class="white" id="id-text2">后台管理</span>
              </h1>
              <h4 class="blue" id="id-company-text">&copy; hermit网络科技</h4>
            </div>

            <div class="space-6"></div>

            <div class="position-relative">
              <div id="login-box" class="login-box visible widget-box no-border">
                <div class="widget-body">
                  <div class="widget-main">

                    <div class="space-6"></div>

                    <form>
                      <fieldset>
                        <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input type="text" class="form-control" placeholder="请输入账号" v-model="userInfo.username"/>
															<i class="ace-icon fa fa-user"></i>
														</span>
                        </label>

                        <label class="block clearfix">
														<span class="block input-icon input-icon-right">
															<input v-bind:type="type" class="form-control" placeholder="请输入密码" v-model="userInfo.password"/>
<!--															<i class="ace-icon fa fa-lock"></i>-->
                              <i class="ace-icon fa fa-eye" v-on:click="changeType()" v-show="type==='text'"></i>
                              <i class="ace-icon fa fa-eye-slash" v-on:click="changeType()" v-show="type==='password'"></i>
														</span>
                        </label>

                        <label class="block clearfix">
                          <span class="block input-icon input-icon-right">
                            <div class="input-group">
                              <input type="text" class="form-control" placeholder="请输入验证码" v-model="userInfo.imageCode">
                              <span class="input-group-addon" id="basic-addon2">
                                <img v-on:click="loadImageCode()" id="image-code" alt="验证码">
                              </span>
                            </div>
                          </span>
                        </label>

                        <div class="space"></div>

                        <div class="clearfix">
                          <label class="inline">
                            <input v-model="remember" type="checkbox" class="ace"/>
                            <span class="lbl">记住密码</span>
                          </label>

                          <button type="button"
                                  v-on:click="login()"
                                  class="width-35 pull-right btn btn-sm btn-primary">
                            <i class="ace-icon fa fa-key"></i>
                            <span class="bigger-110">登陆</span>
                          </button>
                        </div>

                        <div class="space-4"></div>
                      </fieldset>
                    </form>
                  </div><!-- /.widget-main -->
                </div><!-- /.widget-body -->
              </div><!-- /.login-box -->
            </div><!-- /.position-relative -->

            <div class="navbar-fixed-top align-right">
              <a id="btn-login-dark" href="#">欢迎</a>
            </div>
          </div>
        </div><!-- /.col -->
      </div><!-- /.row -->
    </div><!-- /.main-content -->
  </div><!-- /.main-container -->
</template>

<script>

  export default {
    name: 'login',
    data () {
      return {
        userInfo:{},
        remember:false,
        LOCAL_KEY_LOGIN_USER:LOCAL_KEY_LOGIN_USER,
        type:"password",
        imageCodeToken:"",
      };
    },
    mounted:function (){
      let _this = this;
      $('body').remove('no-skin');
      $('body').attr('class', 'login-layout blur-login');
      let rememberUser = LocalStorage.get(LOCAL_KEY_LOGIN_USER);
      if(rememberUser){
        _this.userInfo = rememberUser;
        // console.log("缓存中的账号密码:"+ _this.userInfo.username +"/" + _this.userInfo.password);
      }
      _this.loadImageCode();
    },
    methods:{
      login() {
       let _this = this;
       // let passwordShow = _this.userInfo.password;
       let md5 = hex_md5(_this.userInfo.password);
       let rememberUser = LocalStorage.get(LOCAL_KEY_LOGIN_USER) || {};
       if(md5!==rememberUser.md5){
         //加密
         _this.userInfo.password = hex_md5(_this.userInfo.password + KEY);
       }
       _this.userInfo.imageCodeToken = _this.imageCodeToken;

        Loading.show();
        _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/userInfo/login',_this.userInfo).then((response)=>{
          Loading.hide();
         let resp = response.data;
          if(resp.success){
           console.log(resp.content);
            let loginUser = resp.content;
            Tool.setLoginUser(resp.content);
            //保存当前登陆信息
            console.log("是否记住账号密码："+ _this.remember);
            if(_this.remember){
              let md5 = hex_md5(_this.userInfo.password);
              LocalStorage.set(LOCAL_KEY_LOGIN_USER,{
                username:loginUser.username,
                password:_this.userInfo.password,
                md5:md5,
              });
            }else{
              LocalStorage.set(LOCAL_KEY_LOGIN_USER,null);
            }

            _this.$router.push("welcome");
          }else{
            toast.warning(resp.message);
            _this.userInfo.password = "";
            _this.loadImageCode();
          }
       })

      },
      changeType(){
        let _this = this;
        if(_this.type === "password"){
          _this.type = "text";
       }else{
         _this.type = "password";
       }
     },
      loadImageCode(){
        let _this = this;
        _this.imageCodeToken = Tool.uuid(8);
        $('#image-code').attr('src',process.env.VUE_APP_SERVER + '/system/admin/kaptcha/image-code/' + _this.imageCodeToken);
      }
  }
}
</script>
<style scoped>
  .input-group-addon {
    padding: 0;
  }
</style>